#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//long long getMaxFunctionValue(vector<int>& receiver, long long k) {
//	long long ret = 0;
//	int n = receiver.size();
//	auto Count = [&](int x)->long long
//	{
//		int tmp = k;
//		long long ret = x;
//		while (tmp--)
//		{
//			x = receiver[x];
//			ret += x;
//		}
//		return ret;
//	};
//	for (int i = 0; i < n; i++)
//	{
//		ret = max(ret, Count(i));
//	}
//	return ret;
//}
class TreeAncestor {
	vector<vector<int>> pa;
public:
	TreeAncestor(int n, vector<int>& parent) {
		pa.resize(n, vector<int>(32, -1));
		for (int i = 0; i < n; i++)
		{
			pa[i][0] = parent[i];
		}
		for (int j = 0; j < 32; j++)
		{
			for (int i = 0; i < n; i++)
			{
				int p = pa[i][j];
				if (p != -1)
					pa[i][j + 1] = pa[p][j];
			}
		}
	}

	int getKthAncestor(int node, int k) {
		for (int i = 0; i < 32; i++)
		{
			if ((k >> i) & 1)
				node = pa[node][i];
			if (node < 0)
				break;
		}
		return node;
	}
};
long long getMaxFunctionValue(vector<int>& receiver, long long k) {
	int n = receiver.size();
	vector<vector<pair<int, long long>>> pa(n, vector<pair<int, long long>>(32, { -1,0 }));
	for (int i = 0; i < n; i++)
	{
		pa[i][0] = { receiver[i],receiver[i] };
	}
	for (int i = 0; i < 32; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int p = pa[j][i].first;
			if (p != -1)
			{
				pa[j][i + 1].first = pa[p][i].first;
				pa[j][i + 1].second = pa[j][i].second + pa[p][i].second;
			}
		}
	}
	auto Count = [&](int x)->long long
	{
		int tmp = k;
		long long cnt = 0;
		for (int i = 0; i < 32; i++)
		{
			if ((tmp >> i) & 1)
			{
				cnt += pa[x][i].second;
			}
		}
		return cnt;
	};
	long long ret = 0;
	for (int i = 0; i < n; i++)
	{
		ret = max(ret, Count(i));
	}
	return ret;
}
int main()
{

	return 0;
}